﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Faust.Andrew.MTG
{
	/// <summary>
	/// Interaction logic for PagingControl.xaml
	/// </summary>
	public partial class PagingControl : UserControl
	{
        public event EventHandler<PageChangedEventArgs> PageChangedEvent;

        private bool _suppressEvent;

        public bool IsZeroBased { get; private set; }

        private int _page;
        public int Page
        {
            get
            {
                if (IsZeroBased)
                {
                    return _page - 1;
                }
                else
                {
                    return _page;
                }
            }
        }

        private int _maxPages;
        public int MaxPages
        {
            get
            {
                if (IsZeroBased)
                {
                    return _maxPages - 1;
                }
                else
                {
                    return _maxPages;
                }
            }
        }

        protected void FirePageChangedEvent(int page)
        {
            if (PageChangedEvent != null)
            {
                if (IsZeroBased)
                {
                    PageChangedEvent(this, new PageChangedEventArgs(page - 1));
                }
                else
                {
                    PageChangedEvent(this, new PageChangedEventArgs(page));
                }
            }
        }

        /// <summary>
        /// Set the current and maximum page
        /// </summary>
        /// <param name="page"></param>
        /// <param name="maxPages"></param>
        /// <param name="isZeroBased">If the source uses page 0 as the first page, IsZeroBased should be set to true. This allows the control
        /// to display Page 1 or X, instead of Page 0 of. It will return the proper 0 based page number automatically.</param>
        public void SetPages(int page, int maxPages, bool isZeroBased)
        {
            if (maxPages < page) { throw new ArgumentOutOfRangeException("maxPages should be >= to page"); }
            if (maxPages < 0 || page < 0) { throw new ArgumentOutOfRangeException("Page and maxPages can't be less than 0"); }

            _suppressEvent = true;

            IsZeroBased = isZeroBased;

            if (IsZeroBased)
            {
                _maxPages = maxPages + 1;
                _page = page + 1;
            }

            lblTotal.Content = _maxPages.ToString();

            cmbPage.Items.Clear();
            for (int i = 1; i <= _maxPages; i++)
            {
                cmbPage.Items.Add(i);
            }

            cmbPage.SelectedItem = _page;

            _suppressEvent = false;
        }
        
		public PagingControl()
		{
			this.InitializeComponent();
		}

        private void UpdatePage(int newPage)
        {
            if (newPage < 1) { return; }
            if (newPage > _maxPages) { return; }
            if (newPage == _page) { return; }

            _page = newPage;

            _suppressEvent = true;
            cmbPage.SelectedItem = _page;
            _suppressEvent = false;

            FirePageChangedEvent(_page);
        }

		private void btnFirst_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            UpdatePage(0);
		}

		private void btnPrevious_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            UpdatePage(_page - 1);
		}

		private void btnNext_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            UpdatePage(_page + 1);
		}

		private void btnLast_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            UpdatePage(_maxPages);
		}

		private void cmbPage_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
            if (_suppressEvent) { return; }

            if (cmbPage.SelectedItem is int)
            {
                UpdatePage((int)cmbPage.SelectedItem);
            }
		}
	}
}